package backend

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"yak.admin/src/entity"
	"yak.admin/src/middleware"
	"yak.admin/src/module/backend/admin/controller"
	"yak.admin/src/module/backend/admin/repository"
	"yak.admin/src/util"
)

func Routers(group *gin.RouterGroup) {
	group.Any("/logout", func(c *gin.Context) {
		util.RemoveSession(c, util.SessionAdminKey)
		c.Redirect(http.StatusFound, "/login")
	})
	group.GET("/profile", func(c *gin.Context) {
		c.Set("Page", "layui/admin/profile.tpl")
	})
	group.POST("/avatar", controller.UploadAvatar)
	group.POST("/profile", controller.Profile)
	group.POST("/update-password", controller.UpdatePassword)

	// 超级管理员
	group.GET("/supers", func(c *gin.Context) {
		c.Set("Page", middleware.Page{Title: "超级管理员", Linker: "supers", Header: "管理员列表", Tpl: "layui/admin/supers.tpl",})
	})
	group.POST("/super/list", func(c *gin.Context) {
		var adminSearch repository.AdminRepository
		data, count := adminSearch.FindSuperAll(c)
		middleware.Datalist(c, data, count)
	})
	group.GET("/super/form", func(c *gin.Context) {
		c.Set("Page", "layui/admin/super_form.tpl")
	})
	group.POST("/super/create", controller.SuperCreate)
	group.POST("/super/reset-pwd", controller.ResetPassword)
	group.DELETE("/super/del", controller.DeleteAdmin)

	// 普通管理员
	group.GET("/admins", func(c *gin.Context) {
		admin := c.MustGet(util.AdminKey).(entity.Admin)
		var title string
		switch admin.Type {
		case util.TypeDrug:
			title = "管理员（药品）"
			break
		case util.TypeDevice:
			title = "管理员（医疗器械）"
			break
		default:
			title = "管理员"
		}
		c.Set("Page", middleware.Page{Title: title, Linker: "admins",  Header: "管理员列表", Tpl: "layui/admin/admins.tpl",})
	})
	group.POST("/admin/list", func(c *gin.Context) {
		var adminSearch repository.AdminRepository
		admin := c.MustGet(util.AdminKey).(entity.Admin)
		data, count := adminSearch.FindNormalAll(c, admin.Type)
		middleware.Datalist(c, data, count)
	})
	group.GET("/admin/form", func(c *gin.Context) {
		c.Set("Page", "layui/admin/admin_form.tpl")
	})
	group.POST("/admin/create", controller.NormalCreate)
	group.POST("/admin/reset-pwd", controller.ResetPassword)
	group.DELETE("/admin/del", controller.DeleteAdmin)

	return
}
